package com.encyclopedia.service.impl;

import com.encyclopedia.domain.PlantCrop;
import com.encyclopedia.mapper.PlantCropMapper;
import com.encyclopedia.mapper.PlantCropRelationMapper;
import com.encyclopedia.result.JsonRequest;
import com.encyclopedia.result.exception.ServiceException;
import com.encyclopedia.result.exenum.RequestException;
import com.encyclopedia.service.PlantCropService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author MaLiang
 * @version 1.0
 * @date 2024-05-05 5:59
 */
@Service
public class PlantCropServiceImpl implements PlantCropService {

    @Autowired
    private PlantCropMapper plantCropMapper;

    @Autowired
    private PlantCropRelationMapper plantCropRelationMapper;

    @Override
    public JsonRequest getCropInfo(PlantCrop plantCrop) {
        List<PlantCrop> cropInfo = plantCropMapper.getCropInfo(plantCrop);
        return JsonRequest.success(cropInfo);
    }

    @Override
    public int insertCrop(PlantCrop plantCrop) {
        int i = plantCropMapper.insertCrop(plantCrop);
        if (i < 1) {
            throw new ServiceException(RequestException.INSERT_ERROR);
        }
        return i;
    }

    @Override
    public int deleteCrop(Long id) {
        int i = plantCropMapper.deleteCrop(id);
        if (i < 1) {
            throw new ServiceException(RequestException.DELETE_ERROR);
        }
        plantCropRelationMapper.deleteCrop(id);
        return 0;
    }

    @Override
    public int updateCrop(PlantCrop plantCrop) {
        int i = plantCropMapper.updateCrop(plantCrop);
        return i;
    }

    @Override
    public JsonRequest getInfoId(Long id) {
        PlantCrop plantCrop = plantCropMapper.getInfoId(id);
        return JsonRequest.success(plantCrop);
    }

}
